//
//  XMBodyPartTableViewCell.m
//  XiaoMiao
//
//  Created by Jim huang on 2018/4/24.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMBodyPartTableViewCell.h"
#import "KBaseCollectionView.h"
#import "XMCameraTypeLayout.h"

#define numberCountInRow 4


@interface XMBodyPartTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) KBaseCollectionView * collectionView;

@property (nonatomic,strong) UIPageControl * pageControl;

@end

@implementation XMBodyPartTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}

-(void)setUI{
    
    UIView * marginView = [[UIView alloc]init];
    marginView.backgroundColor = kBgColor;
    [self.contentView addSubview:marginView];
    
    [marginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView);
        make.height.equalTo(@10);
    }];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.bottom.equalTo(self.contentView);
        make.height.equalTo(@20);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(marginView.mas_bottom);
        make.right.left.equalTo(self.contentView);
        make.bottom.equalTo(self.pageControl.mas_top);
    }];
}


#pragma mark ----collection----

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    XMCameraBodyPartCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[XMCameraBodyPartCollectionViewCell className] forIndexPath:indexPath];

    
    if (indexPath.row >= self.listData.count) {
        cell.hidden = YES;
        cell.model = nil;
    }else{
        
        cell.hidden = NO;
        XMTypeListClassifyModel * classifyModel = self.listData[indexPath.row];
        cell.model = classifyModel;
        cell.itemSelected = classifyModel.isSelected;
    }
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (self.listData.count%(numberCountInRow*2)) {
        return (self.listData.count/(numberCountInRow*2)+1)*(numberCountInRow*2);
    }else{
        return self.listData.count;
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat  LRMargin = 15;
    
    NSInteger itemW = (kScreenWidth-LRMargin*numberCountInRow*2)/numberCountInRow;
    
    return CGSizeMake(itemW, KRealValue(70));
}


-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(10,15,10,15);//分别为上、左、下、右
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    XMTypeListClassifyModel * classifyModel = self.listData[indexPath.row];

    BOOL canSelect = NO;
    if (!classifyModel.isSelected) {

        canSelect = YES;
        
    }else{
        for (XMTypeListClassifyModel * model in self.listData) {
            if (model.isSelected && model !=classifyModel) {
                canSelect = YES;
            }
        }
    }
    
    if (canSelect) {
        classifyModel.isSelected = !classifyModel.isSelected;
        
        if (self.selectItemBlock) {
        self.selectItemBlock(indexPath.row,classifyModel.isSelected,NO);
        }
        [self.collectionView reloadData];
    }else{
        

        [KLProgressHUD showErrorHUDWithMessage:@"至少选择一个"];
        if (self.selectItemBlock) {
        self.selectItemBlock(indexPath.row,classifyModel.isSelected,YES);
        }
    }

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat scrollViewW = scrollView.frame.size.width;
    
    CGFloat x = scrollView.contentOffset.x;
    
    int page = (x + scrollViewW/2)/scrollViewW;
    
    _pageControl.currentPage = page;
    
}


#pragma mark ----getting&setting----
-(UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectZero];
        _pageControl.currentPageIndicatorTintColor = kMainBlueColor;
        _pageControl.pageIndicatorTintColor = HEX_RGB(0x858585);
        [self.contentView addSubview:_pageControl];
    }
    
    return _pageControl;
}
//列表
-(KBaseCollectionView *)collectionView{
    
    if (!_collectionView) {
        XMCameraTypeLayout *layout = [[XMCameraTypeLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        layout.itemCountPerRow = numberCountInRow;
        layout.rowCount = 2;
        layout.minimumLineSpacing = 30;
        layout.minimumInteritemSpacing = 10;
        _collectionView = [[KBaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = kWhiteColor;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [self.contentView addSubview:_collectionView];
        
        [_collectionView registerClass:[XMCameraBodyPartCollectionViewCell class] forCellWithReuseIdentifier:[XMCameraBodyPartCollectionViewCell className]];
        
        
    }
    return _collectionView;
}


-(void)setListData:(NSArray *)listData{
    _listData = listData;
    
    if (listData.count/(numberCountInRow*2)) {
        _pageControl.numberOfPages = listData.count/(numberCountInRow*2)+1;
    }else{
        _pageControl.numberOfPages = listData.count/(numberCountInRow*2);

    }
    [self.collectionView reloadData];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

@interface XMCameraBodyPartCollectionViewCell ()
//
@property (nonatomic,strong) UILabel * titleLabel;

@property (nonatomic,strong) UIImageView * picImageView;


@end

@implementation XMCameraBodyPartCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

-(void)setUI{
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@18);
        make.left.right.bottom.equalTo(self);
    }];
    [self.picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.centerX.equalTo(self);
        make.bottom.equalTo(self.titleLabel.mas_top).offset(-5);
        make.width.equalTo(self.picImageView.mas_height);
    }];
    

    
}

#pragma mark ----getting&setting----
//图片
-(UIImageView *)picImageView{
    if (!_picImageView) {
        _picImageView = [[UIImageView alloc]init];
        [_picImageView setImage:kImage(@"ic_common_avatar_default")];
        
        [self addSubview:_picImageView];
    }
    return _picImageView;
}


//角度文字
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = kTextBlackColor;
        _titleLabel.font = kFont(14);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

-(void)setItemSelected:(BOOL)itemSelected{
    _itemSelected = itemSelected;
    
    if (itemSelected) {
        [self.picImageView sd_setImageWithURL:[NSURL URLWithString:self.model.tag] placeholderImage:nil];

    }else{
        [self.picImageView sd_setImageWithURL:[NSURL URLWithString:self.model.imageUrl] placeholderImage:nil];
    }
    
}


-(void)setModel:(XMTypeListClassifyModel *)model{
    _model = model;
    self.titleLabel.text = model.name;
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:nil];
}


@end
